﻿<link href="@Href("~/Modules/NineSeas/Styles/authoring.books.css")" rel="stylesheet" type="text/css"/>
<link href="@Href("~/Modules/NineSeas/Styles/books.summary.css")" rel="stylesheet" type="text/css"/>
<div class="books-container">   
    <div class="clear: both;"></div>
    @if (Model.Books != null && Model.Books.Count > 0) {
        <ul class="contentItems">
        @foreach (var book in Model.Books) {
            var imageUrl = String.IsNullOrWhiteSpace(book.ImageUrl) ?
                Href("~/Modules/NineSeas/Images/placeholder-book.png")
                : book.ImageUrl;
            var tagsHtml = new List<IHtmlString>();
            foreach (string t in book.Tags)
            {
                if (tagsHtml.Any())
                {
                    tagsHtml.Add(new HtmlString(" "));
                }
                tagsHtml.Add(Html.ActionLink(t, "Search", "Home", new { area = "Orchard.Tags", tagName = t }, new { }));
            }
            <li>
                <div class="book-leftPane">
                    <a href="@Url.Action("Index", "BookView", new {name = book.NameOnUrl})">
                        <img src="@imageUrl" alt="@book.Name" class="book-image"/>
                    </a>
                </div>
                <div class="book-rightPane">
                    <div class="book-rightPane-header">                           
                        <div class="book-commands">
                            <a href="@Url.Action("Edit", "Books", new {book.Id})" title="@T("Edit")">@T("Edit")</a>@T(" | ")
                            <a href="@Href(String.Format("~/Authoring/Books/Book/{0}/Chapters", book.Id))" title="@T("Chapters")">@book.ChapterCount @T("Chapters")</a>
                            @T(" | ")
                            <a href="@Url.Action("Delete", "Books", new { book.Id, returnUrl = Request.Url.PathAndQuery })" class="delete-command">@T("Delete")</a>
                        </div>
                    </div>
                    <div class="book-rightPane-content">      
                        <div class="book-name">
                            <a href="@Url.Action("Index", "BookView", new { name = book.NameOnUrl })">@book.Name</a>
                            @if (!book.VersionRecord.Published)
                            {
                                var message = book.DraftCount > 0 ? T("Book has drafts - {0} modified chapter(s) are unpublished", book.DraftCount) : T("Book has draft.");
                                @Display(New.Controls_HelpPopup(Message: message, Width: 180))
                            }
                        </div>
                        <div class="book-author" data-id="@book.AuthorId"></div>
                        <div class="book-createdDate">@T("Created") @book.CreatedDate</div>
                        <div class="book-lastUpdatedDate">@T("Last Updated") @book.LastUpdatedDate</div>
                        <div class="book-introduction">
                            @Html.Excerpt((string)book.Introduction, 300)
                        </div>
                        <div class="tags">@foreach(var htmlString in tagsHtml) { @htmlString }</div>  
                    </div>
                    <div class="book-rightPane-footer"></div>
                </div>
            </li>
        } 
        </ul>
    } else {
        <b><i>@T("You haven't posted any book. Please use the button on the bottom to create a new one.")</i></b>
    }
</div>
@{
    Func<dynamic, dynamic> linkCreator = i => Url.Action("Index", "Books", new
    {
        @Expression = Model.Search != null && Model.Search.Expression != null ? Model.Search.Expression : "",
        @currentPage = i,
        @pageSize = Model.Paging == null ? 10 : Model.Paging.PageSize
    });
}
<div class="books-paging" @(Model.Paging.TotalRecords > 0 ? "" : "style=display:none;") data-url="@Request.Url.PathAndQuery">    
    @Display(New.Books_Pager(
        Paging: Model.Paging,
        LinkCreator: linkCreator
    ))
</div>
<script type="text/javascript">
    $(document).ready(function () {   
        // author details ajax rendering
        var authorIds = [];
        $('.book-author').each(function () {
            if ($(this).attr("data-id") != ''
                && $(this).attr("data-id") != null
                && authorIds.indexOf($(this).attr("data-id")) == -1) {
                authorIds.push($(this).attr("data-id"));
            }
        });

        $.each(authorIds, function (index, item) {
            $.ajax({
                url: '@Href("~/nineseas/api/authors/")' + item,
                cache: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                complete: function (data) {
                    var author = $.parseJSON(data.responseText);

                    var authorLink = '<a href="@Href("~/")Authors/Author/' + item + '/Details">' + author.DisplayName + '</a>';
                    $('.book-author[data-id=' + item + ']').html(authorLink);
                }
            });
        });

        // delete confirmation
        $(".delete-command").click(function (e) {
            e.preventDefault();

            var $this = $(this);

            $.confirm('@T("Are you sure you want to delete?")', function (result) {
                if (result) {
                    $.ajax($this.attr("href"), {
                        cache: false,
                        type: "POST",
                        data: {"__RequestVerificationToken": "@Html.AntiForgeryTokenValueOrchard()"},
                        success: function (data) {
                            $("#nineseas-reader-footer .books-paging").remove();

                            var activeTabIndex = $("#authoring-tabs").tabs("option", "active") + 1;
                            $(".ui-tabs-panel:nth-of-type(" + activeTabIndex + ")").html(data);

                            $.message('@T("The book was deleted successfully!")');
                        },
                        error: function (jqXHR, textStatus, errorThrown ) {
                            $.alert(errorThrown);
                        }
                    });
                }
            });
        });

        // command bar
        var barOptions = {
            resetElement: true,
            left: [],
            right: []
        };

        var showPaging = @(Model.Paging.TotalRecords > 0 ? "true" : "false");
        if (showPaging) {            
            barOptions.right.push($(".books-paging:last-of-type"));

            var pagingOpening = false;
            var panel = $(".books-paging ul");
            $(".books-paging .pager-current, .books-paging .total-pages").on("click", function () {
                var newState = panel.css('display') == 'none' ? 'block' : 'none';

                pagingOpening = newState == 'block';
                if (pagingOpening) {
                    panel.slideToggle('slow');
                }
            });

            $(".books-paging ul a").on("click", function (e) {
                var $this = $(this);
                e.preventDefault();

                $.ajax($this.attr("href"), {
                    cache: false,
                    success: function (data) {
                        $("#nineseas-reader-footer .books-paging").remove();

                        var activeTabIndex = $("#authoring-tabs").tabs("option", "active") + 1;
                        $(".ui-tabs-panel:nth-of-type(" + activeTabIndex + ")").html(data);

                        $(".books-paging").attr("data-url", $this.attr("href"));
                    },
                    error: function (jqXHR, textStatus, errorThrown ) {
                        $.alert(errorThrown);
                    }
                });
            });

            $(document).click(function () {
                if (!pagingOpening) {
                    if (panel.css('display') != 'none') {
                        panel.slideToggle('slow');
                    }
                }
                pagingOpening = false;
            });
        }
        barOptions.right.push({
            id: 'createButton',
            type: 'a', // button | a
            href: '@Url.Action("Edit", "Books")',
                caption: '@T("Create New Book")'
        });
        $.commandBar('#nineseas-reader-footer', barOptions);
    });
</script>